import { defineStore } from 'pinia'
import { ApiGetBanner, ApiLogin, ApiGetVipBanner } from '@/api/menulist'
import axios from 'axios'

export const userMenuListStore = defineStore('userMenuList', {
  state: () => ({
    menuList: [] as any,
    btnList: [] as any,
    token: '' || localStorage.getItem('token'),
    refresh_token: '',
    is_admin: false || localStorage.getItem('is_admin'),
  }),
  actions: {
    async getMenuListStore() {
      const res: any = await ApiGetBanner()
      if (res.code === 20000) {
        this.menuList = res.data.menuTreeList
        this.btnList = res.data.buttonList
      }
      console.log(res, '列表')
      return res
    },
    async getMenuListStoreVip() {
      const res: any = await ApiGetVipBanner()
      if (res.code === 20000) {
        this.menuList = res.data.menuTreeList
        this.btnList = res.data.buttonList
      }
      console.log(res, '列表')
      return res
    },
    async getMenuListStore1() {
      console.log(this.is_admin, 'this.is_admin')
      const { data }: any = await axios.get('api/permission?user=' + this.is_admin)
      this.menuList = data.data.menuTreeList
      this.btnList = data.data.buttonList
      console.log(data, '列表')
    },
    async getLogin() {
      const { data } = await ApiLogin()
      console.log(data, '登录')
      this.is_admin = data.is_admin
      this.token = data.token
      this.refresh_token = data.refresh_token
      localStorage.setItem('token', data.token)
      localStorage.setItem('is_admin', data.is_admin)
    },
  },
  getters: {
    hasPermission: (state) => (permission: any) => {
      return state.btnList.some((item: any) => item.includes(permission))
    },
  },
})
